//
//  WeekHandleItem.m
//  CoolMove
//
//  Created by CA on 15/10/30.
//  Copyright © 2015年 CA. All rights reserved.
//

#import "WeekHandleItem.h"
#import "NSDate+CM_DateTools.h"

@interface WeekHandleItem ()
@property (nonatomic, strong) NSMutableArray *weekSportRecordItems;
@property (nonatomic, strong) NSString *startEndDateStr;        // 周一至周日
@property (nonatomic, strong) NSString *averageCalories;        // 平均能量消耗
@property (nonatomic, strong) NSString *averageSwingCount;      // 平均击球次数
@property (nonatomic, strong) NSString *averageSportDuration;   // 平均活跃时间
@property (nonatomic, strong) NSString *amountCalories;         // 总能量消耗
@property (nonatomic, strong) NSString *amountSwingCount;       // 总击球次数
@property (nonatomic, strong) NSString *maxSpeed;               // 最大拍速
@property (nonatomic, strong) NSString *amountSportDuration;    // 周活跃时间
@property (nonatomic, strong) NSDate *computeDate;
@property (nonatomic, assign) NSInteger maxYValue;
@property (nonatomic, assign) NSInteger minYValue;
@end

@implementation WeekHandleItem

- (instancetype)initEmptyItemWithDate:(NSDate *)date
{
    self = [super init];
    if (self) {
        _startEndDateStr = [NSString stringWithFormat:@"%@-%@", [date convertMonthDayByWeekDay:WeekDayMonday], [date convertMonthDayByWeekDay:WeekDaySunday]];
        _averageCalories = @"0";
        _averageSwingCount = @"0";
        _averageSportDuration = @"0";
        _amountCalories = @"0";
        _amountSwingCount = @"0";
        _maxSpeed = @"0";
        _amountSportDuration = @"0";
    }
    return self;
}

- (NSMutableArray *)weekSportRecordItems
{
    if (_weekSportRecordItems == nil) {
        _weekSportRecordItems = [@[] mutableCopy];
    }
    
    return _weekSportRecordItems;
}

- (NSString *)startEndDateStr
{
    if (_startEndDateStr == nil) {
        SportRecordItem *sportRecordItem = [self.weekSportRecordItems lastObject];
        if (sportRecordItem != nil) {
            NSString *monday = [[sportRecordItem date] convertMonthDayByWeekDay:WeekDayMonday];
            NSString *sunday = [[sportRecordItem date] convertMonthDayByWeekDay:WeekDaySunday];
            _startEndDateStr = [NSString stringWithFormat:@"%@-%@", monday, sunday];
        }
    }
    
    return _startEndDateStr;
}

- (NSString *)amountCalories
{
    if (_amountCalories == nil) {
        NSInteger amoutCalories = 0;
        for (SportRecordItem *item in self.weekSportRecordItems) {
            amoutCalories += item.calories;
        }
        _amountCalories = [@(amoutCalories) stringValue];
    }
    
    return _amountCalories;
}

- (NSString *)amountSwingCount
{
    if (_amountSwingCount == nil) {
        NSInteger amoutSwingCount = 0;
        for (SportRecordItem *item in self.weekSportRecordItems) {
            amoutSwingCount += [item amountOfCount];
        }
        _amountSwingCount = [@(amoutSwingCount) stringValue];
        
    }
    
    return _amountSwingCount;
}

- (NSString *)amountSportDuration
{
    if (_amountSportDuration == nil) {
        NSInteger minutes = 0;
        for (SportRecordItem *item in self.weekSportRecordItems) {
            minutes += item.exerciseTimeInterval;
        }
        _amountSportDuration = [NSString stringWithFormat:@"%.1f", roundf(minutes/6.0f)/10.0f];
    }
    
    return _amountSportDuration;
}

- (NSString *)averageSportDuration
{
    CGFloat averageDuration = [self.amountSportDuration floatValue]/[self.weekSportRecordItems count];
    return [NSString stringWithFormat:@"%.1f", averageDuration];
}

- (NSString *)averageSwingCount
{
    NSInteger averageSwingCount = [self.amountSwingCount integerValue]/[self.weekSportRecordItems count];
    return [@(averageSwingCount) stringValue];
}

- (NSString *)averageCalories
{
    NSInteger averageCalories = [self.amountCalories integerValue]/[self.weekSportRecordItems count];
    return [@(averageCalories) stringValue];
}

- (NSString *)maxSpeed
{
    if (_maxSpeed == nil) {
        NSInteger maxSpeed = 0;
        for (SportRecordItem *item in self.weekSportRecordItems) {
            if (maxSpeed < item.maxSpeed) {
                maxSpeed = item.maxSpeed;
            }
        }
        
        _maxSpeed = [@(maxSpeed) stringValue];
    }
    
    return _maxSpeed;
}

- (NSArray *)fillSportRecordItems
{
    NSMutableArray *fillSportRecordItems = [@[] mutableCopy];
    for (NSInteger index = 2; index <= 8; index++) {
        id sportRecordItemResult = [self sportRecordItemByWeekDay:index%7];
        [fillSportRecordItems addObject:sportRecordItemResult];
    }
    
    return [fillSportRecordItems copy];
}

- (id)sportRecordItemByWeekDay:(NSInteger)weekDay
{
    for (SportRecordItem *item in self.weekSportRecordItems) {
        if ([item date].weekday == weekDay) {
            return item;
        }
    }
    
    return @0;
}

#pragma mark - Public Method

- (void)addSportRecordItem:(SportRecordItem *)sportRecordItem
{
    [self.weekSportRecordItems addObject:sportRecordItem];
    self.computeDate = sportRecordItem.date;
}

- (NSArray *)sportDurations
{
    NSMutableArray *sportDuraitons = [@[] mutableCopy];
    for (id item in [self fillSportRecordItems]) {
        if ([item isKindOfClass:[SportRecordItem class]]) {
            NSInteger sportDuration = ((SportRecordItem *)item).exerciseTimeInterval * 15;
            [sportDuraitons addObject:@(((SportRecordItem *)item).exerciseTimeInterval * 15)];
            if (self.maxYValue < sportDuration) {
                self.maxYValue = sportDuration;
            }
            
        } else {
            [sportDuraitons addObject:item];
        }
    }
    
    return [sportDuraitons copy];
}

- (NSArray *)sportSwings
{
    NSMutableArray *sportSwings = [@[] mutableCopy];
    for (id item in [self fillSportRecordItems]) {
        if ([item isKindOfClass:[SportRecordItem class]]) {
            NSInteger swings = [((SportRecordItem *)item) amountOfCount];
            [sportSwings addObject:@(swings)];
            
            if (self.maxYValue < swings) {
                self.maxYValue = swings;
            }
            
        } else {
            [sportSwings addObject:item];
        }

    }
    
    return [sportSwings copy];
}

- (NSArray *)sportCalories
{
    NSMutableArray *sportCalories = [@[] mutableCopy];
    for (id item in [self fillSportRecordItems]) {
        if ([item isKindOfClass:[SportRecordItem class]]) {
            NSInteger calories = ((SportRecordItem *)item).calories;
            [sportCalories addObject:@(calories)];
            
            if (self.maxYValue < calories) {
                self.maxYValue = calories;
            }
            
        } else {
            [sportCalories addObject:item];
        }

    }
    
    return [sportCalories copy];
}

- (BOOL)hasSportValue
{
    if ([self.amountCalories integerValue] > 0 || [self.amountSwingCount integerValue] > 0 || [self.maxSpeed integerValue] > 0 || [self.amountSportDuration integerValue] > 0) {
        return YES;
    }
    
    return NO;
}

@end
